<?php
namespace Controllers;

use Models\CommentModel;

class CommentController extends CommonController {
    public function add() {
        if (!$this->isLogin()) return $this->responseJson(401, '尚未登录，请先登录');
        $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
        $content = isset($_POST['content']) ? trim($_POST['content']) : '';
        if (empty($content)) return $this->responseJson(400, '评价内容必须填写');
        $data = [
            'content' => $content,
            'post_id' => $post_id,
            'user_id' => $_SESSION['user_id']
        ];
        $commentModel = new CommentModel();
        if ($commentModel->add($data)) {
            return $this->responseJson(200, '评论发布成功');
        } else {
            return $this->responseJson(400, '评论发布失败');
        }
    }

    public function evaluate() {
        if (!$this->isLogin()) return $this->responseJson(401, '尚未登录，请先登录');
        $comment_id = isset($_POST['comment_id']) ? intval($_POST['comment_id']) : 0;
        if (empty($comment_id)) return $this->responseJson(400, '帖子参数错误');
        $type = isset($_POST['type']) ? intval($_POST['type']) : 1;
        $commentModel = new CommentModel();
        if ($commentModel->evaluate($type, $comment_id)) {
            return $this->responseJson(200, '操作成功');
        } else {
            return $this->responseJson(400, '操作失败');
        }
    }
}